/*
 * Ken Anderson, 2009
 */

#include <stdlib.h>
#include "Visualizer.h"
#include "Graph.h"
#include "Search.h"
#include <queue>
#include <vector>
#include <fstream>
#include <time.h>

// Loads up a map, and compares A* search using various different heuristics.
void TestSearches(Map &map)
{
	// Load map
	Graph* graph = dynamic_cast<Graph*>(&map);

	// Create heuristics to test.
	std::vector<Heuristic*> heuristics;
	heuristics.push_back( new ZeroHeuristic(graph) );
	heuristics.push_back( new ManhattanDistanceHeuristic(graph) );
	heuristics.push_back( new ResumableAdditiveHeuristic(graph, false, true) );

	// Create search algorithms
	std::vector<Search*> searches;
	for( unsigned int i=0; i<heuristics.size(); i++ )
		searches.push_back( new AStar( graph, heuristics[i] ) );
	
	// Set start and goal nodes
	Node* start = graph->GetRandomNode();
	Node* goal = graph->GetRandomNode();

	for( unsigned int i=0; i<searches.size(); i++ )
	{
		searches[i]->Init( start, goal );
	}

	// Run the Visualizer
	Visualizer visualizer;
	visualizer.Initialize( searches );
	visualizer.VisualizerLoop();
	visualizer.Cleanup();

	// Free memory
	while( searches.size() > 0) {
		delete( searches.back() );
		searches.pop_back();
	}
	while( heuristics.size() > 0) {
		delete heuristics.back();
		heuristics.pop_back();
	}
}

int main(int argc, char *argv[])
{
	const char * mapArray = 
		"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@@@@@@...@@@@@@@@.@@@.@@@@@@@@"
		"@.......................@....@"
		"@....@.@@@@@@.@@@@@@@@@.@....@"
		"@....@....@......@....@.@....@"
		"@....@....@......@....@......@"
		"@....@....@......@......@....@"
		"@....@....@@@@@@.@@@@@@.@....@"
		"@....@....@...........@.@@@@@@"
		"@....@....@...........@.@....@"
		"@....@....@...........@.@....@"
		"@....@@@@@@...........@......@"
		"@....@....@@@@@@@@@@@@@.@....@"
		"@....@....@.....@.......@....@"
		"@....@....@.....@.....@.@@@@@@"
		"@....@....@.....@.....@.@....@"
		"@....@....@.....@.....@.@....@"
		"@....@.@@@@@@.@@@@@@@@@.@....@"
		"@....@..................@....@"
		"@@@@@@...@@@@@@@@.@@@.@@@@@.@@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@......@.....@......@........@"
		"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
	Map map(30,30,mapArray);
	TestSearches(map);

    return 0;
}
